package oppsa.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.util.TreeMap;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import oppsa.controller.Translate;
import oppsa.model.ProjectDB;
import oppsa.model.UserDB;
import oppsa.util.Session;

import org.jdesktop.swingx.JXFrame;


public class CreateManagerView extends JXFrame {
	private static final long serialVersionUID = 1L;
	private JPanel panel;
	private JLabel lblWelcome;
	private JLabel lblFirstName;
	private JLabel lblLastName;
	private JLabel lblMail;
	private JLabel lblCompany;
	private JButton buttonSave;
	private JButton source;
	private JTextField ManagerFirstName;
	private JTextField ManagerLastName;
	private JTextField ManagerMail;
	private JTextField ManagerCompany;
	private JLabel oblig1;
	private JLabel oblig2;

	private UserDB manager;
	private TreeMap<Integer, String> UsersMap;

	private ProjectDB newProj;
	private int id; /////
	private String name;
	private Boolean test;
	private JXFrame homeView;
	private ActionDetailsView adv;
	private JButton btnCancel ;


	//private ProjectDetailsView pv;


	private Translate t = new Translate();

	public CreateManagerView(final boolean nouveau, final ActionDetailsView adv) {
		addWindowListener(closeWindow);
		adv.addWindowFocusListener(clickhomeview);
		setIconImage(Toolkit.getDefaultToolkit().getImage(CreateManagerView.class.getResource("/LOGO_FONCE.png")));
		this.adv = adv;
		setStartPosition(StartPosition.CenterInScreen);
		this.setMinimumSize(new Dimension(420, 300));
		setResizable(false);
		if (nouveau) {
			this.setTitle(t.getLocalizedText("tlCreateUser"));
		} else {
			this.setTitle(t.getLocalizedText("tlCreateUser"));
		}
		panel = new JPanel();
		panel.setLayout(null);
		panel.setBackground(Color.white);
		getContentPane().add(panel);
		manager = new UserDB();
		UsersMap = new TreeMap<Integer, String>();
		if (!nouveau) {
			btnCancel = new JButton();
		}




		setDefaultCloseOperation( DO_NOTHING_ON_CLOSE );
		WindowAdapter closeWindow = new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				closeAjoutManager();
			}

		};
		addWindowListener(closeWindow);

		if (nouveau) {
			lblWelcome = new JLabel(t.getLocalizedText("lblNewUser"));
		} else {
			lblWelcome = new JLabel(t.getLocalizedText("lblNewUser"));
		}
		 
		lblWelcome.setFont(new Font("Tahoma", Font.PLAIN, 18));
		lblWelcome.setForeground(Color.BLACK);
		lblWelcome.setBounds(103, 11, 218, 30);
		panel.add(lblWelcome);

		lblFirstName = new JLabel(t.getLocalizedText("lblFirstName"));
		//lblPleaseCreateA.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblFirstName.setBounds(10, 75, 110, 30);
		panel.add(lblFirstName);
		lblLastName = new JLabel(t.getLocalizedText("lblLastName"));
		//lblPleaseCreateA.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblLastName.setBounds(10, 105, 110, 30);
		panel.add(lblLastName);
		lblMail = new JLabel(t.getLocalizedText("lblMail"));
		//lblPleaseCreateA.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblMail.setBounds(10, 135, 110, 30);
		panel.add(lblMail);
		lblCompany = new JLabel(t.getLocalizedText("lblCompany"));
		//lblPleaseCreateA.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblCompany.setBounds(10, 165, 110, 30);
		panel.add(lblCompany);

		ManagerFirstName = new JTextField();	
		ManagerFirstName.setPreferredSize(new Dimension(150,30));
		ManagerLastName = new JTextField();	
		ManagerLastName.setPreferredSize(new Dimension(150,30));
		ManagerMail = new JTextField();	
		ManagerMail.setPreferredSize(new Dimension(150,30));
		ManagerCompany = new JTextField();	
		ManagerCompany.setPreferredSize(new Dimension(150,30));


		ManagerFirstName.setBounds(120, 75, 160, 30);
		ManagerLastName.setBounds(120, 105, 160, 30);
		ManagerMail.setBounds(120, 135, 160, 30);
		ManagerCompany.setBounds(120, 165, 160, 30);

		panel.add(ManagerFirstName);
		panel.add(ManagerLastName);
		panel.add(ManagerMail);
		panel.add(ManagerCompany);
		oblig1 = new JLabel("*");
		oblig1.setBounds(282, 75, 20, 20);
		oblig1.setFont(new Font("Tahoma", Font.PLAIN, 14));
		panel.add(oblig1);
		oblig2 = new JLabel("*");
		oblig2.setBounds(282, 105, 20, 20);
		oblig2.setFont(new Font("Tahoma", Font.PLAIN, 14));
		panel.add(oblig2);
		if (!nouveau){
		helpTailleBouton(btnCancel,"Cancel",305, 180);
		btnCancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (nouveau ) {
					JOptionPane.showMessageDialog(CreateManagerView.this, t.getLocalizedText("lblManagerInfo"));
				} else {
					closeAjoutManager();
				}
			}
		});

		btnCancel.setBorder(null);
		panel.add(btnCancel);
		btnCancel.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("Cancel"))));
		}
		buttonSave = new JButton("");
		buttonSave.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {




				if (!ManagerFirstName.getText().equals("") && !ManagerLastName.getText().equals("")){
						
						
						manager = new UserDB();

						if(!manager.insert(ManagerFirstName.getText(),ManagerLastName.getText(),ManagerCompany.getText(),ManagerMail.getText(), "",Session.projectSequence)){
							JOptionPane.showMessageDialog(null, t.getLocalizedText("errorinsertUser"));	
						}
						
						setVisible(false);
						adv.setVisible(true);
						adv.setEnabled(true);
						adv.toFront();
						if (nouveau){
							homeView = new HomeView();
							homeView.setVisible(true);
						} else {
							CreateManagerView.this.adv.updateOwner(ManagerLastName.getText()+" "+ManagerFirstName.getText());
						}
						
				}else{

					if (ManagerFirstName.getText().equals("")) {
						ManagerFirstName.setBackground(Color.getHSBColor(255, 255, 50));
					}
					if (ManagerLastName.getText().equals("")) {
						ManagerLastName.setBackground(Color.getHSBColor(255, 255, 50));
					}

				}

				//prv.setVisible(true);

			}

			@Override
			public void mouseEntered(MouseEvent e) {
				source = (JButton) e.getSource();
				source.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("Submit"))));
			}

			@Override
			public void mouseExited(MouseEvent e) {
				source = (JButton) e.getSource();
				source.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("Submit"))));
			}


		});

		buttonSave.setBorder(null);
		helpTailleBouton(buttonSave,"Submit", 305, 145);
		panel.add(buttonSave);
	}

	private void closeAjoutManager(){
		setVisible(false);
		adv.setVisible(true);
		adv.setEnabled(true);
		adv.toFront();	
	}

	private void helpTailleBouton(JButton btn, String icon, int x, int y  ){
		ImageIcon img = new ImageIcon(getClass().getResource(t.getLocalizedText(icon)));
		int tailleW = img.getIconWidth();
		int tailleH = img.getIconHeight();
		btn.setBounds(x,y, tailleW, tailleH);

		btn.setIcon(img);

	}
	WindowAdapter closeWindow = new WindowAdapter() {
		@Override
		public void windowClosing(WindowEvent arg0) {
			
			setVisible(false);
			adv.setVisible(true);
			adv.setEnabled(true);
			adv.toFront();	
			
		}
	};
	WindowFocusListener  clickhomeview = new  WindowFocusListener () {

		@Override
		public void windowGainedFocus(WindowEvent arg0) {
			// TODO Auto-generated method stub
			toFront();
		}

		@Override
		public void windowLostFocus(WindowEvent arg0) {
			// TODO Auto-generated method stub
			
		}
	};
}
